import router from "./router"
import store from "./store"

// 白名单
const whileList = ['/login']

// 路由前置守卫
// router.beforeEach( async(to, from, next) => {
//     if(store.getters.token){
//         if(to.path === '/login') {
//             next('/')
//         }else{
//             next()
//         }
//     }else{
//         if(whileList.indexOf(to.path) > -1){
//             next()
//         }else{
//             next('/login')
//         }
//     }
// })

router.beforeEach( async(to, from) => {
    try {
        if(store.getters.token){
            if(to.path === '/login') {
                // return {
                //     name:'index',
                //     query: {redirectFrom: to.fullPath}
                // }
                return '/'
            } else {
                // 判断用户资料是否获取
                // 若不存在用户信息，则需要获取用户信息
                console.log('hasuserinfo',store.getters.hasUserInfo)
                if(!store.getters.hasUserInfo) {
                    await store.dispatch('user/getUserInfo')
                    console.log(11)
                }else{
                    console.log(222)
                }
                // console.log('userInfo', store.user.userInfo)
                // console.log('hasUserInfo',store.getters.hasUserInfo)
            }
            return;
        }else{
            if(whileList.indexOf(to.path) === -1){
                // return {
                //     name:'login',
                //     query: {redirectFrom: to.fullPath}
                // }
                return '/login'
            }

            return;
        }        
    } catch (e) {
        throw e
    }

})